package com.atguigu.webadmin.controller.lease;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.LeaseAgreement;
import com.atguigu.model.enums.LeaseStatus;
import com.atguigu.webadmin.service.LeaseAgreementService;
import com.atguigu.webadmin.vo.agreement.AgreementQueryVo;
import com.atguigu.webadmin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "租约管理")
@RestController
@RequestMapping("/admin/agreement")
@CrossOrigin
public class LeaseAgreementController {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Operation(summary = "保存或修改租约信息")
    @Parameters({
            @Parameter(name = "leaseAgreement",description = "租约信息对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Object> saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        boolean b = leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok(b);
    }

    @Operation(summary = "根据条件分页查询租约列表")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "size",description = "当前页面展示数量",in = ParameterIn.QUERY),
            @Parameter(name = "queryVo",description = "租约vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("page")
    public Result<IPage<AgreementVo>> pageAgreementByQuery(@RequestParam long current,
                                           @RequestParam long size, AgreementQueryVo queryVo) {
        //创建分页对象，并将查询条件封装进分页对象
        IPage<AgreementVo> voIPage = new Page<>(current,size);
        //根据条件进行分页查询
        IPage<AgreementVo> agreementVoIPage = leaseAgreementService.pageAgreementByQuery(voIPage,queryVo);
        return Result.ok(agreementVoIPage);
    }

    @Operation(summary = "根据id查询租约信息")
    @Parameters({
            @Parameter(name = "id",description = "租约信息id",in = ParameterIn.DEFAULT)
    })
    @GetMapping(name = "getById")
    public Result<AgreementVo> getAgreementById(@RequestParam Long id) {
        AgreementVo agreementVo = leaseAgreementService.getAgreementById(id);
        return Result.ok(agreementVo);
    }

    @Operation(summary = "根据id删除租约信息")
    @Parameters({
            @Parameter(name = "id",description = "租约信息id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("removeById")
    public Result<Boolean> removeById(@RequestParam Long id) {
        boolean b = leaseAgreementService.removeById(id);
        return Result.ok(b);
    }

    @Operation(summary = "根据id更新租约状态")
    @Parameters({
            @Parameter(name = "id",description = "租约信息id",in = ParameterIn.DEFAULT),
            @Parameter(name = "status",description = "租约状态更新",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusById")
    public Result<Boolean> updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        //创建Lambda更新包装类
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(LeaseAgreement::getId,id);
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,status);
        boolean update = leaseAgreementService.update(lambdaUpdateWrapper);
        return Result.ok(update);
    }

}

